package com.encryption.controls
{
	import com.encryption.rpc.cache.FileLevelManager;
	import com.encryption.rpc.model.FileLevel;
	
	import mx.collections.ArrayCollection;
	import mx.containers.FormItem;
	import mx.controls.RadioButton;
	import mx.core.UIComponent;
	
	public class FileLevelFormItem extends FormItem
	{
		public function FileLevelFormItem()
		{
			super();
			label="密级：";
			direction = "horizontal";
			
			initUI();
		}
		
		public function reset():void
		{
			for each(var comp:UIComponent in getChildren()){
				if(comp is RadioButton){
					RadioButton(comp).selected = true;
					break;
				}
			}
		}
		
		private function initUI():void
		{
			super.createChildren();
			
			FileLevelManager.instance.addInitListener(function():void{
				var list:ArrayCollection = FileLevelManager.instance.getAll();
				
				var count:int = 1;
				for each(var item:FileLevel in list){
					var btn:RadioButton = new RadioButton();
					if(count == 1)
						btn.selected = true;
					btn.label = item.toString();
					btn.groupName = "level";
					addChild(btn);
					
					count++;
				}
			});
		}
		
		public function getFileLevel():int
		{
			var selectedLabel:String = "";
			for each(var comp:UIComponent in getChildren()){
				if(comp is RadioButton && RadioButton(comp).selected){
					selectedLabel = RadioButton(comp).label;
					break;
				}
			}
			
			var level:FileLevel = FileLevelManager.instance.getByString(selectedLabel) as FileLevel;
			return level != null ? level.id : 1;
		}
	}
}